package cn.fsd.service.impl;

import cn.fsd.domain.Order;
import cn.fsd.domain.Purchase_bill;
import cn.fsd.domain.Task;
import cn.fsd.mapper.CommodityMapper;
import cn.fsd.mapper.Purchase_billMapper;
import cn.fsd.query.BaseQuery;
import cn.fsd.service.BillService;
import cn.fsd.vo.BillBean;
import cn.fsd.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    Purchase_billMapper purchase_billMapper;

    @Autowired
    CommodityMapper commodityMapper;

    @Override
    public PageBean<Purchase_bill> findByPage(BaseQuery query) {
        //构建结果对象
        PageBean<Purchase_bill> pageBean = new PageBean<>();

        //开启分页
        PageHelper.startPage(query.getPageNow(),query.getPageSize());

        //分页查询得到当页的集合
        List<Purchase_bill> billList = purchase_billMapper.findByPage(query);

        //创建一个分页的实体PageInfo
        PageInfo<Purchase_bill> pageInfo = new PageInfo<>(billList);

        pageInfo.setList(pageInfo.getList().stream().map(bill -> {
            BillBean bean = new BillBean(bill);
            Long cost = commodityMapper.selectByPrimaryKey(bill.getCommodity_id()).getCost();//成本价
            bean.setCost(cost);
            Integer settle_num = bill.getPurchase_num()-bill.getReturn_num();//结算数量
            bean.setSettle_num(settle_num);
            bean.setSettle_amount(cost*settle_num);//结算金额
            return bean;
        }).collect(Collectors.toList()));

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }
}
